library(data.table)
load("data/analysis_data.RData")
set.seed(90864001)
respondent_candidate_data <- respondent_candidate_data[
  n_attention_fails <= 1 &
    support_2016 != "Trump" &
    trump_least_preferred == 1
]
ranking_means <- respondent_candidate_data[, .(
  mean_ranking = mean(ranking),
  mean_ranking_electability_last = mean(ranking[electability_last == 1]),
  mean_ranking_preferences_last = mean(ranking[electability_last == 0]),
  pct_first_choice_electability_last = mean(ranking[electability_last == 1] == 1),
  pct_first_choice_preferences_last = mean(ranking[electability_last == 0] == 1)
  ), candidate][order(mean_ranking), .(candidate,
    mean_ranking = round(mean_ranking, 1),
    mean_ranking_electability_last = round(mean_ranking_electability_last, 1),
    mean_ranking_preferences_last = round(mean_ranking_preferences_last, 1),
    ranking_electability_last = rank(mean_ranking_electability_last),
    ranking_preferences_last = rank(mean_ranking_preferences_last),
    pct_first_choice_electability_last = round(100*pct_first_choice_electability_last, 1),
    pct_first_choice_preferences_last = round(100*pct_first_choice_preferences_last, 1),
    round(100 * pct_first_choice_electability_last - 100 * pct_first_choice_preferences_last, 1)
    )]
ranking_means
# subject_ids <- respondent_candidate_data[, unique(subject_id)]
# subject_data_list <- lapply(subject_ids, function(ID) {
#   respondent_candidate_data[subject_id == ID,
#     .(ranking, candidate, electability_last)]
# })
# boot <- function(i) {
#   boot_subject_ids <- sample(seq_along(subject_data_list), replace = TRUE)
#   boot_data <- rbindlist(lapply(boot_subject_ids,
#     function(ID) subject_data_list[[ID]]))
#   boot_data[, .(
#     mean_ranking = mean(ranking),
#     mean_ranking_electability_last = mean(ranking[electability_last == 1]),
#     mean_ranking_preferences_last = mean(ranking[electability_last == 0]),
#     pct_first_choice_electability_last = mean(ranking[electability_last == 1] == 1),
#     pct_first_choice_preferences_last = mean(ranking[electability_last == 0] == 1)
#   ), candidate][order(mean_ranking), .(
#     candidate,
#     mean_ranking = round(mean_ranking, 3),
#     mean_ranking_electability_last = round(mean_ranking_electability_last, 3),
#     mean_ranking_preferences_last = round(mean_ranking_preferences_last, 3),
#     pct_first_choice_electability_last = round(pct_first_choice_electability_last, 3),
#     pct_first_choice_preferences_last = round(pct_first_choice_preferences_last, 3)
#   )]
# }
# boots <- rbindlist(lapply(1:1000, boot))
# boots[, quantile(V2, .025), candidate]
# boots[, quantile(V2, .975), candidate]

# library(rstanarm)
# options(mc.cores = parallel::detectCores())
# set.seed(2)
# model <- stan_glmer(1 * (ranking == 1) ~ (electability_last | candidate),
#   data = respondent_candidate_data, family = binomial,
#   prior = default_prior_coef(family),
#   prior_intercept = default_prior_intercept(family),
#   prior_aux = exponential(autoscale = TRUE),
#   prior_covariance = decov(),
#   prior_PD = FALSE)
# confint_model <- confint(model)


respondent_data <- respondent_data[
  n_attention_fails <= 1 &
    support_2016 != "Trump" &
    trump_least_preferred == 1]
respondent_data[ranking_biden == 1, first_choice := "Biden"]
respondent_data[ranking_sanders == 1, first_choice := "Sanders"]
respondent_data[ranking_warren == 1, first_choice := "Warren"]
respondent_data[ranking_buttigieg == 1, first_choice := "Buttigieg"]
respondent_data[ranking_bloomberg == 1, first_choice := "Bloomberg"]
respondent_data[ranking_klobuchar == 1, first_choice := "Klobuchar"]
respondent_data[ranking_yang == 1, first_choice := "Yang"]
respondent_data[, first_choice := factor(first_choice,
  levels = c("Biden", "Bloomberg", "Buttigieg", "Klobuchar",
    "Sanders", "Warren", "Yang"))]

respondent_candidate_data[, t.test(ranking == 1 ~ order)$p.value, candidate]

library(nnet)
model <- nnet::multinom(first_choice ~ order, data = respondent_data)
perm <- function(x) {
  coef(nnet::multinom(first_choice ~ order, respondent_data[, .(first_choice, order = sample(order))]))
}
set.seed(2)
perms <- lapply(1:1000, perm)
coefs <- do.call(rbind, lapply(perms, function(x) x[, 2]))
model
sapply(1:6, function(x) 2 * min(
  mean(coefs[, x] > coef(model)[x, 2]),
  mean(coefs[, x] < coef(model)[x, 2])))

# mean(coefs[, 1] > coef(model)[1, 2])
# mean(coefs[, 1] > coef(model)[1, 2])
# mean(coefs[, 1] > coef(model)[1, 2])
# mean(coefs[, 1] > coef(model)[1, 2])
# apply(coefs, 2, quantile, .975)
#
# set.seed(2)
# perms <- sapply(1:1000, function(i)
#   respondent_candidate_data[,
#     .(candidate,
#       electability_last = sample(electability_last),
#       win = as.numeric(ranking >=7))][,
#       mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#       candidate][, sum(V1^2)])
# test_stat <- respondent_candidate_data[,
#   .(candidate, electability_last,
#     win = as.numeric(ranking >= 7))][,
#       mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#       candidate][, sum(V1^2)]
# 1-mean(test_stat > perms)
#
#
#
# set.seed(2)
# perms <- sapply(1:1000, function(i) respondent_candidate_data[,
#   .(candidate, electability_last = sample(electability_last),
#     win = as.numeric(ranking >=7))][,
#   mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#   candidate][, sum(V1^2)])
# test_stat <- respondent_candidate_data[,
#   .(candidate, electability_last,
#     win = as.numeric(ranking >= 7))][,
#       mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#       candidate][, sum(V1^2)]
# 1-mean(test_stat > perms)
#
#
# perms <- sapply(1:1000, function(i) respondent_candidate_data[,
#   .(candidate, electability_last = sample(electability_last),
#     win = as.numeric(ranking <= 1))][,
#       mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#       candidate][, sum(V1^2)])
# test_stat <- respondent_candidate_data[,
#   .(candidate, electability_last,
#     win = as.numeric(ranking <= 1))][,
#       mean(win[electability_last == 1]) - mean(win[electability_last == 0]),
#       candidate][, sum(V1^2)]
# 1-mean(test_stat > perms)
#
# respondent_candidate_data[, .(
#   mean_ranking = mean(ranking),
#   mean_ranking_electability_last = mean(ranking[electability_last == 1]),
#   mean_ranking_preferences_last = mean(ranking[electability_last == 0]),
#   pct_first_choice_electability_last = mean(ranking[electability_last == 1] == 1),
#   pct_first_choice_preferences_last = mean(ranking[electability_last == 0] == 1)
# ), candidate][order(mean_ranking), .(candidate,
#   mean_ranking = round(mean_ranking, 3),
#   mean_ranking_electability_last = round(mean_ranking_electability_last, 3),
#   mean_ranking_preferences_last = round(mean_ranking_preferences_last, 3),
#   ranking_electability_last = rank(mean_ranking_electability_last),
#   ranking_preferences_last = rank(mean_ranking_preferences_last),
#   pct_first_choice_electability_last = round(pct_first_choice_electability_last, 3),
#   pct_first_choice_preferences_last = round(pct_first_choice_preferences_last, 3),
#   pct_first_choice_electability_last - pct_first_choice_preferences_last
# )]

# set up environment ----
library(data.table)
library(ggplot2)
library(cowplot)
library(parallel)
source("R/00-functions.R")
load("data/analysis_data.RData")
RNGkind("L'Ecuyer-CMRG")
set.seed(356024460)

# regressions ----
#   heterogeneity by gender ----
fmla_list <- list(-ranking ~ electability_last * candidate - electability_last - 1)

#     main dataset ----
data_main <- respondent_candidate_data[main == 1]
id_main <- data_main[, unique(subject_id)]
n_main <- length(id_main)
data_list_main <- lapply(id_main, function(i) {
  data_main[subject_id == i]
})
models_main <- lapply(fmla_list, lm, data = data_main)
boot <- function(i) {
  boot_ids <- sample(n_main, n_main, replace = TRUE)
  boot_data <- rbindlist(lapply(boot_ids, function(i) {
    data_list_main[[i]]
  }))
  boot_models <- lapply(fmla_list, lm, data = boot_data)
  lapply(boot_models, coef)
}
boots_main <- mclapply(1:1000, boot, mc.cores = 6)
boot_coefs_main <- rbindlist(lapply(boots_main, function(y) as.data.table(rbind(y[[1]]))))

probs <- c(.025, .975)
boot_coefs_main[, round(quantile(`electability_last:candidateBiden`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateBloomberg`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateButtigieg`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateKlobuchar`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateSanders`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateWarren`, probs), 1)]
boot_coefs_main[, round(quantile(`electability_last:candidateYang`, probs), 1)]
